package com.app.newiptv.activity;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.app.newiptv.base.MyApplication;
import com.app.newiptv.bean.AppInfo;
import com.app.newiptv.utils.APPLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wendal on 2017/3/23.
 */

public class GetAllPackage {

    /**
     * 得到手机中所有的应用程序信息
     * @return
     */
    public  List<AppInfo> loadAppInfos(){
        PackageManager pm = MyApplication.getContext().getPackageManager();
        //创建要返回的集合对象
        List<AppInfo> appInfos = new ArrayList<AppInfo>();
        //获取手机中所有安装的应用集合
        List<ApplicationInfo> applicationInfos = pm.getInstalledApplications(0);

        APPLog.printInfo("applicationInfos.size()------------------------"+applicationInfos.size());
        //遍历所有的应用集合
        for(ApplicationInfo info : applicationInfos){
            if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {	// 非系统应用
                AppInfo appInfo = new AppInfo();
                //获取应用程序的图标
                Drawable app_icon = info.loadIcon(pm);
                APPLog.printInfo("app_icon------------------------"+app_icon);
                appInfo.setApp_icon(app_icon);

                //获取应用的名称
                String app_name = info.loadLabel(pm).toString();
                APPLog.printInfo("aapp_name------------------------"+app_name);
                appInfo.setApp_name(app_name);

                //获取应用的包名
                String packageName = info.packageName;
                APPLog.printInfo("packageName------------------------"+packageName);
                appInfo.setPackagename(packageName);
                try {
                    //获取应用的版本号
                    PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
                    String app_version = packageInfo.versionName;
                    APPLog.printInfo("app_version------------------------"+app_version);
                    appInfo.setApp_version(app_version);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                //判断应用程序是否是用户程序
//			boolean isUserApp = filterApp(info);
//			appInfo.setUserApp(isUserApp);
                appInfos.add(appInfo);
            }
        }
        return appInfos;
    }

    /**
     * 获取安装程序的包名
     * @return
     */
    public static List<String> getAppPackageNames(Context context) {
        List<String> packageNames = new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        //获取手机中所有安装的应用集合
        List<ApplicationInfo> applicationInfos = pm.getInstalledApplications(0);
        for (ApplicationInfo info : applicationInfos) {
            if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {    // 非系统应用
                //获取应用的包名
                String packageName = info.packageName;
                packageNames.add(packageName);
            }
        }
        return packageNames;
    }
}
